﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class Manage_CG_GiftProductOperate : System.Web.UI.Page
{
    private string act = Utils.RQ_str("act").ToLower();
    private int id = Utils.RQ_int("id");
    public string productclass = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        clsAdmin adm = clsAdmin.getCookieAdmin();
        if (adm == null)
        {
            Utils.WrAlertGo("您尚未成功登陆，请成功登陆后再进行正常操作！", "../Login.aspx");
        }

        bindContrl();
        if (!this.IsPostBack)
            bindData();
    }

    private void bindContrl()
    {
        DLprice.DataSource = clsGiftProductPriceSpan.GetCollection();
        DLprice.DataValueField = "GiftProductPriceSpanID";
        DLprice.DataTextField = "GiftProductPriceSpanName";
        DLprice.DataBind();
    }

    private void bindData()
    {
        if (act == "edit")
        {
            ltMgTitle.Text = "修改产品";
            ltPathTitle.Text = "[修改产品]";
            ltOperTitle.Text = "修改产品";
            clsGiftProduct cls = new clsGiftProduct(id);
            tbxGiftProductName.Text = cls.GiftProductName;
            tbxGiftProductInfo.Text = cls.GiftProductInfo;
            tbxGiftProductMinOrder.Text = cls.GiftProductMinOrder.ToString();
            tbxGiftProductOuterTime.Text = cls.GiftProductOuterTime.ToString();
            tbxGiftProductUnit.Text = cls.GiftProductUnit;
            DLprice.SelectedValue = cls.GiftProductPriceSpanID.ToString();
            List<int> ls = clsGiftProductClass.GetCodeById(cls.GiftProductClassID);
            for (int i = 0; i < ls.Count; i++)
            {
                productclass += string.Format(" dv{0}=\"{1}\"", i + 1, ls[i]);
            }
            btnSave.Text = " 修 改 ";
        }
        else
        {
            ltMgTitle.Text = "添加产品";
            ltPathTitle.Text = "[添加产品]";
            ltOperTitle.Text = "添加产品";
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string name = tbxGiftProductName.Text.Trim();
        if (name == "")
        {
            Utils.WrAlertGoBack("标注*号为必填不可为空！");
        }
        clsGiftProduct cls = new clsGiftProduct(id);
        cls.GiftProductName = name;
        cls.GiftProductInfo = tbxGiftProductInfo.Text;
        cls.GiftProductMinOrder = Convert.ToInt32(tbxGiftProductMinOrder.Text);
        cls.GiftProductOuterTime = Convert.ToInt32(tbxGiftProductOuterTime.Text);
        cls.GiftProductUnit = tbxGiftProductUnit.Text.Trim();
        cls.GiftProductPriceSpanID = Convert.ToInt32(DLprice.SelectedValue);
        int proClassId = Utils.RF_int("giftclass");
        
        if(proClassId!=0)
            cls.GiftProductClassID = proClassId;

        if (act.ToLower() == "edit")
        {
            clsGiftProduct.Update(cls);
        }
        else
        {
            clsGiftProduct.Insert(cls);
        }
        Response.Redirect("GiftProductList.aspx", true);
    }
}